***Adding measurement error to location data to protect subject confidentiality while allowing for consistent estimation of exposure effects
***The Journal of the Royal Statistical Society: Series C (Applied Statistics)

***REPLICATION DO FILE 1: GENERATE DISTANCE TO FACILITY DATASET

***Mahesh Karra, David Canning, Ryoko Sato
***February 29, 2020

version 13

***STEP 0: STORE WORKING FILES AND SETTING WORKING DIRECTORY

***Make sure that all do files and data files that are provided with this replication package are stored in the same folder.

***Users will need to tailor the file directory pathway to the specific filepath where the replication package is stored.
***To use the correct file directory, replace "maindir" with the appropriate file path under which your Dropbox folder is stored.

if "`c(username)'"=="mvkarra" {
                global maindir "C:\Users\mvkarra\Dropbox\Harvard 2015-2016\POP CENTER\Distance Paper - Part 2\DOCUMENTS\JOURNAL SUBMISSIONS\JRSS SERIES C\Replication Files"
}
else if "`c(username)'"=="[another username here]" {
                global maindir "[same as above]"
}
 
***INSTALL GLOBDIST COMMAND IN STATA, IN CASE IT IS NOT INSTALLED
capture ssc install globdist, replace

***GENERATE MINIMUM DISTANCE FROM THE GRID POINT TO EACH HF

***USE WOMAN AND HF GPS DATA: DATASET 1
use "$maindir\DATA_1_WOMAN_GPS_2_29_20.dta", clear

***SET SEED
set seed 22071987

***Sort Woman data by Woman ID
destring womanid, gen(wid)
format wid %20.0g
sort wid

***APPEND GRID POINT DATA: DATASET 2
append using "$maindir\DATA_2_ARUSHA_GRID_GPS_2_29_20.dta"


***CALCULATE DISTANCE FROM EACH GRID POINT TO HEALTH FACILITY (HF) LOCATION

local l=1
while `l'<=820 {
local j=latitude[`l']
local k=longitude[`l']
globdist distcHF`l' , lat0(`j') lon0(`k') latvar(grid_lati) lonvar(grid_longi)
local l=`l'+1
}


drop if womanid~=""
drop womanid-km_hf

***GENERATE LOG DISTANCE VARIABLES
foreach var of varlist distcHF1- distcHF820 { 
replace `var'=log(`var')
}

***SAVE DATASET AS DATASET 3: ERROR FIX WITH LOG HF DISTANCES
save "$maindir\DATA_3_ERROR_FIX_HF_2_29_20.dta", replace
